/**
 * Copyright (C) 2009+ Spark Zheng
 *
 * @file        src/monitor.c 
 * @date        2010-04-05
 * @brief	server_framework
 *		monitor, based on tcp protocol, as a common protocol-module.
 *		it has four purposes(maybe):
 *		-1. collect system runtime data (for gslb/health-check)
 *		-2. collect server-process runtime arguments (for argument-statistics)
 *		-3. monitor server-process runtime arguments, do callbacks on data-exception
 *		-4. online runtime arguments adjusting.
 *
 * @see         dserver
 * @history
 */

#include <dlib/mempool.h>

#include "utils.h"
#include "prot_types.h"
#include "connection.h"
#include "monitor.h"
#include "mont_types.h"

/** global variable in prot_tcp.c */
extern prot_ops_t tcp_ops;

int monitor_init(dserver_t *dc, dserver_protocol_t *protocols)
{
	prot_node_t *monitor;
	
	DLOG_DEBUG(LOG_MAIN, "dserver enters in monitor_init function.");
	if (dc == NULL || protocols == NULL) {
		DLOG_ERR(LOG_MAIN, "monitor_init parameters error.");
		return -E_ARGS_ILLEGAL;
	}
	
	monitor = dmalloc(sizeof(*monitor));
	protocols->p_monitor = monitor;
	
	monitor->p_name = MONITOR_NAME_STR;
	monitor->p_sfd = -1;
	monitor->p_settings.p_type = PROTOCOL_TCP;
	monitor->p_settings.p_host = dc->settings->mont_host;
	monitor->p_settings.p_port = dc->settings->mont_port;
	monitor->p_settings.p_timeout = dc->settings->mont_timeout;
	monitor->p_settings.p_priority = dc->settings->mont_priority;

	monitor->p_ops = tcp_ops;
	monitor->p_main_event = NULL;
	monitor->p_base.p_tcp_main_cpool = NULL;
	//monitor->p_base.p_tcp_base.p_main_cpool = NULL;
	
	monitor->s_ops.p_protocol_load = NULL;
	monitor->s_ops.p_session_init = dserver_monitor_session_init;
	monitor->s_ops.p_session_worker = dserver_monitor_session_worker;
	monitor->s_ops.p_session_exit = dserver_monitor_session_exit;
	monitor->s_ops.p_protocol_unload = NULL;
	
	return RET_SUCCESS;
}

void monitor_exit(prot_node_t *monitor)
{
	DLOG_DEBUG(LOG_MAIN, "dserver enters in monitor_exit function.");
	if (monitor != NULL) {
		dfree(monitor);
		monitor = NULL;
	}
	return;
}

